if (!require(pacman)) install.packages("pacman")
## Loading required package: pacman
pacman::p_load(tidyverse,
sf,
mapview,
here)
# read/export vector data -------------------------------------------------
# read a shapefile (e.g., ESRI Shapefile format)
# `quiet = TRUE` just for cleaner output
(sf_nc_county <- st_read(dsn = here("data/nc.shp"),
quiet = TRUE))
## Simple feature collection with 100 features and 1 field
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -84.32377 ymin: 33.88212 xmax: -75.45662 ymax: 36.58973
## Geodetic CRS: WGS 84
## First 10 features:
## county geometry
## 1 ashe MULTIPOLYGON (((-81.47258 3...
## 2 alleghany MULTIPOLYGON (((-81.23971 3...
## 3 surry MULTIPOLYGON (((-80.45614 3...
## 4 currituck MULTIPOLYGON (((-76.00863 3...
## 5 northampton MULTIPOLYGON (((-77.21736 3...
## 6 hertford MULTIPOLYGON (((-76.74474 3...
## 7 camden MULTIPOLYGON (((-76.00863 3...
## 8 gates MULTIPOLYGON (((-76.56218 3...
## 9 warren MULTIPOLYGON (((-78.30849 3...
## 10 stokes MULTIPOLYGON (((-80.02545 3...
# export as shp file ------------------------------------------------------
st_write(sf_nc_county,
dsn = here("data/sf_nc_county.shp"),
append = FALSE)
## Deleting layer `sf_nc_county' using driver `ESRI Shapefile'
## Writing layer `sf_nc_county' to data source
## `/Users/oswaldoguerrero/Documents/GitHub/cw_rgis/data/sf_nc_county.shp' using driver `ESRI Shapefile'
## Writing 100 features with 1 fields and geometry type Multi Polygon.
# export as geopackage
st_write(sf_nc_county,
dsn = here("data/sf_nc_county.gpkg"),
append = FALSE)
## Deleting layer `sf_nc_county' using driver `GPKG'
## Writing layer `sf_nc_county' to data source
## `/Users/oswaldoguerrero/Documents/GitHub/cw_rgis/data/sf_nc_county.gpkg' using driver `GPKG'
## Writing 100 features with 1 fields and geometry type Multi Polygon.
# export as RDS
saveRDS(sf_nc_county,
file = here("data/sf_nc_county.rds"))
#read as RDS
sf_nc_county <- readRDS(file = here("data/sf_nc_county.rds"))
## point data
## as is
sf_site <- readRDS(file = here("data/sf_finsync_nc.rds"))
mapview(sf_site,
col.regions = "black", # point's fill color
legend = FALSE) # disable legend
##take the first ten sites
sf_site10 <- sf_site %>%
slice(1:10)
mapview(sf_site10,
col.regions = "black", # point's fill color
legend = FALSE) # disable legend
##line data
sf_str <- readRDS(here("data/sf_stream_gi.rds"))
##take first 10 sites
sf_str10 <- sf_str %>%
slice(1:10)
mapview(sf_str,
color = "steelblue", # line's color
legend = FALSE) # disable legend
#polygon
mapview(sf_nc_county,
col.regions ="tomato",
legend = FALSE)
##pick guilford county
sf_nc_gi <- sf_nc_county %>%
filter(county == "guilford")
mapview(sf_nc_gi,
col.region = "salmon",
legend = FALSE)
# use ggplot to visualize a map
## not a great map
ggplot() +
geom_sf(data = sf_nc_county) +
geom_sf(data = sf_str) +
geom_sf(data = sf_site)

## a little better
ggplot() +
geom_sf(data = sf_nc_gi) +
geom_sf(data = sf_str)

## read stream line data for Ashe county
sf_str_as <- readRDS(file = here("data/sf_stream_as.rds"))
## check coordinate reference systems
#Crs for sf_str_as:84
#Crs for sf_county:84
print(sf_str_as)
## Simple feature collection with 6361 features and 1 field
## Geometry type: LINESTRING
## Dimension: XY
## Bounding box: xmin: -81.73891 ymin: 36.24445 xmax: -81.24562 ymax: 36.588
## Geodetic CRS: WGS 84
## First 10 features:
## geometry fid
## 1 LINESTRING (-81.32158 36.47... fid000001
## 2 LINESTRING (-81.31133 36.47... fid000002
## 3 LINESTRING (-81.38594 36.45... fid000003
## 4 LINESTRING (-81.39307 36.46... fid000004
## 5 LINESTRING (-81.3454 36.533... fid000005
## 6 LINESTRING (-81.34719 36.53... fid000006
## 7 LINESTRING (-81.3328 36.518... fid000007
## 8 LINESTRING (-81.34964 36.50... fid000008
## 9 LINESTRING (-81.35949 36.53... fid000009
## 10 LINESTRING (-81.35094 36.52... fid000010
print(sf_nc_county)
## Simple feature collection with 100 features and 1 field
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -84.32377 ymin: 33.88212 xmax: -75.45662 ymax: 36.58973
## Geodetic CRS: WGS 84
## First 10 features:
## county geometry
## 1 ashe MULTIPOLYGON (((-81.47258 3...
## 2 alleghany MULTIPOLYGON (((-81.23971 3...
## 3 surry MULTIPOLYGON (((-80.45614 3...
## 4 currituck MULTIPOLYGON (((-76.00863 3...
## 5 northampton MULTIPOLYGON (((-77.21736 3...
## 6 hertford MULTIPOLYGON (((-76.74474 3...
## 7 camden MULTIPOLYGON (((-76.00863 3...
## 8 gates MULTIPOLYGON (((-76.56218 3...
## 9 warren MULTIPOLYGON (((-78.30849 3...
## 10 stokes MULTIPOLYGON (((-80.02545 3...
##map streams and county boundaries
ggplot() +
geom_sf(data = sf_nc_county) +
geom_sf(data = sf_str_as)

##subset county layer to Ashe county and remap
sf_nc_as <- sf_nc_county %>%
filter(county == "ashe")
ggplot() +
geom_sf(data = sf_nc_as) +
geom_sf(data = sf_str_as)
